﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Perceiveit.Xml.Tests
{
    public class PerformanceSet : System.Collections.ObjectModel.KeyedCollection<string,PerformanceSetResult>
    {

        protected override string GetKeyForItem(PerformanceSetResult item)
        {
            return string.Format("{0}:{1}",item.Category, item.Action);
        }

        public bool TryGetResult(string category, string action, out PerformanceSetResult result)
        {
            if (this.Count == 0)
            {
                result = null;
                return false;
            }
            else
            {
                return this.Dictionary.TryGetValue(string.Format("{0}:{1}", category, action), out result);
            }
        }
    }

    public class PerformanceSetResult
    {
        
        public string Category { get; set; }

        public string Action { get; set; }
        [System.ComponentModel.DisplayName("First (milliseconds)")]
        public double First { get; set; }

        public int Count { get; set; }

        [System.ComponentModel.DisplayName("Average ex. 1st (milliseconds)")]
        public double Average { get; set; }

        [System.ComponentModel.DisplayName("Total ex. 1st (milliseconds)")]
        public double Total { get; set; }

        [System.ComponentModel.DisplayName("Minimum (milliseconds)")]
        public double Minimum { get; set; }

        [System.ComponentModel.DisplayName("Maximum (milliseconds)")]
        public double Maximum { get; set; }

        public void AddResult(PerformanceMarker marker)
        {
            if (this.Count == 0)
            {
                First = marker.Duration;
                Count = 1;
                Average = 0.0;
                Minimum = First;
                Maximum = First;
                Total = 0.0;
                Category = marker.Category;
                Action = marker.Action;
            }
            else
            {
                Total += marker.Duration;
                Count++;
                Average = Total / (double)(Count-1);
                if (marker.Duration < Minimum)
                    Minimum = marker.Duration;
                else if (marker.Duration > Maximum)
                    Maximum = marker.Duration;
            }

        }
    }
}
